package structures.xmltable;

import java.io.Serializable;

public class ServiceTableColumn implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2373002576226100308L;
	
	private ServiceTableTypeParameter parentParameter;
	
	private long parentOid;
	
	private String columnName;
	private int index;
	private int size;
	
	boolean willBeDeleted = false;
	
	private boolean kysDefined;
	private boolean kysAnalyzed;
	
	public ServiceTableColumn(ServiceTableTypeParameter parentParameter, String columnName, int index) {
		super();
		this.columnName = columnName;
		this.index = index;
		this.kysAnalyzed = false;
		this.kysDefined = false;
		this.parentParameter = parentParameter;
	}

	public int calcSize(){
		if(columnName == null || columnName.equalsIgnoreCase("OID")){
			size = 110;
		}
		else if(columnName.indexOf("OID") > -1){
			size = 130;
		}
		else if(columnName.indexOf("ACIKLAMA") > -1 
				|| columnName.indexOf("DESCRIPTION") > -1
				|| columnName.indexOf("ADRES") > -1){
			size = 370;
		}
		else {
			size = columnName.length() * 6 - 30;
			if(size < 100)
				size = 100;
		}
		return size;
	}
	public void delete(){
		willBeDeleted = true;
	}
	
	public long getParentOid() {
		return parentOid;
	}

	public int getSize(){
		if(size == 0){
			calcSize();
		}
		return size;
	}
	public String getColumnName() {
		return columnName;
	}
	public void renameColumn(String columnName) {
		this.columnName = columnName;
	}
	public void setColumnName(String columnName) {
//		this.columnName = columnName;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public void setSize(int size){
		this.size = size;
	}
	public boolean isKysDefined() {
		return kysDefined;
	}
	public void setKysDefined(boolean kysDefined) {
		this.kysDefined = kysDefined;
	}
	public boolean isKysAnalyzed() {
		return kysAnalyzed;
	}
	public void setKysAnalyzed(boolean kysAnalyzed) {
		this.kysAnalyzed = kysAnalyzed;
	}
	public void setParentOid(long parentOid) {
		this.parentOid = parentOid;
	}
	
	public void loadParentParameter(ServiceTableTypeParameter parentParameter){
		this.parentParameter = parentParameter;
	}
	public String retrieveParentParameterName(){
		if(this.parentParameter != null)
			return parentParameter.getBagKey();
		return null;
	}
	
	public void selectForUpdate(){
		if(this.parentParameter != null)
			this.parentParameter.selectColumnForUpdate(this);
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean sonuc = false;
		if(obj!= null){
			ServiceTableColumn column = (ServiceTableColumn) obj;
			sonuc = column.getColumnName().equalsIgnoreCase(columnName);
		}
		return sonuc || super.equals(obj);
	}
	
	public ServiceTableColumn clone(ServiceTableTypeParameter parentParameter){
		ServiceTableColumn clone = new ServiceTableColumn(parentParameter, columnName, index);
		return clone;
	}
}
